package com.explorati.LeetCode202.happynumber;

import java.util.HashSet;
import java.util.Set;

/**
 * 202 Happy Number 使用HashSet 和solution异曲同工 但性能差
 * 
 * @author explorati
 *
 */
public class Solution1 {
	public boolean isHappy(int n) {
		Set<Integer> set = new HashSet<Integer>();
		set.add(n);
		while (n != 1) {
			int sum = 0;
			while (n != 0) {
				sum += Math.pow(n % 10, 2);
				n = n / 10;
			}
			if (set.contains(sum)) {
				return false;
			}
			set.add(sum);
			n = sum;
		}
		return true;
	}
}
